package lumis.service.wsrp.producer;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.PreRemove;
import javax.persistence.Table;

import lumis.portal.PortalException;
import lumis.portal.authentication.SessionConfig;
import lumis.portal.manager.ManagerFactory;
import lumis.portal.transaction.AbstractTransactionObserver;
import lumis.portal.transaction.ITransaction;
import lumis.portal.transaction.PortalTransactionFactory;
import lumis.portal.user.UserConfig;

import org.hibernate.annotations.GenericGenerator;

/**
 * Internal class that represents a ClonedPortlet.
 * @version $Revision: 13092 $ $Date: 2011-05-28 18:19:06 -0300 (Sat, 28 May 2011) $
 * @since 6.0.0
 */
@Entity
@GenericGenerator(name = "portal-guid", strategy = "lumis.portal.dao.hibernate.GuidGenerator")
@Table(name="lum_WsrpPClonedPortlet")
@SuppressWarnings("serial")
class ClonedPortlet implements Serializable 
{
	
	@Id
	private String id;
	
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="originalPortletId")
	private OfferedPortlet originalPortlet;

	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="registrationHandle")
	private RemoteConsumerRegistration registration;
	
	/**
	 * Returns the originalPortlet to the ClonedPortlet.
	 * @return the portlet from which this cloned was originated.
	 */
	public OfferedPortlet getOriginalPortlet() 
	{
		return originalPortlet;
	}
	/**
	 * Sets the originalPortlet to the ClonedPortlet. 
	 * @param originalPortlet the portlet to set from which the current clonedPortlet was originated.
	 */
	public void setOriginalPortlet(OfferedPortlet originalPortlet) 
	{
		this.originalPortlet = originalPortlet;
	}
	/**
	 * Returns the interface instance id for the current ClonedPortlet.
	 * @return the interface instance id of the cloned portlet.
	 */
	public String getId() 
	{
		return id;
	}
	/**
	 * Sets the interface instance id for the current ClonedPortlet.
	 * @param id the interface instance id of the cloned portlet to set.
	 */
	public void setId(String id) 
	{
		this.id = id;
	}
	/**
	 * Returns the registrationHandle for the current ClonedPortlet.
	 * @return the registrationHandle for this clonedPortlet.
	 */
	public String getRegistrationHandle() 
	{
		return registration.getRegistrationHandle();
	}
	
	/**
	 * Gets the Registration associated to the cloned portlet.
	 * @return RemoteCustomerRegistration registration for the current clonedPortlet.
	 */
	public RemoteConsumerRegistration getRegistration()
	{
		return this.registration;
	}
	
	/**
	 * Sets registration of the clonedPortlet.  Also adds to the registration the cloned portlet.
	 * @param registration registration to be set.
	 */
	public void setRegistration(RemoteConsumerRegistration registration)
	{
		this.registration = registration;
		registration.getClonedPortlets().add(this);
	}

	/*
	 * Removes the would-be orphans from the lum_InterfaceInstace table before removing the clonedPortlet
	 */
	@PreRemove
	void doPreRemove() throws PortalException
	{
		PortalTransactionFactory.getCurrentTransaction().addObserver(new AbstractTransactionObserver()
		{
			@Override
			public boolean beforeCommit() throws PortalException
			{
				ITransaction transaction = PortalTransactionFactory.getCurrentTransaction();
				SessionConfig sessionConfig = ManagerFactory.getAuthenticationManager().impersonate(UserConfig.USER_SYSTEM_ID);
				try
				{
					ManagerFactory.getServiceInterfaceInstanceManager().delete(sessionConfig, id, transaction);
				}
				finally
				{
					ManagerFactory.getAuthenticationManager().endImpersonation(sessionConfig);
				}
				
				return true;
			}
		});
	}
}
